#include "sobelfilter.h"
#include "math.h"

SobelFilter::SobelFilter()
{
}

void SobelFilter::filter(QImage *image){

    int** matriceX;
    matriceX = new int*[3];
    for(int i=0;i<3;i++){
        matriceX[i] = new int[3];
    }

    int** matriceY;
    matriceY = new int*[3];
    for(int i=0;i<3;i++){
        matriceY[i] = new int[3];
    }

    QImage* imageX = new QImage(*image);

    matriceX[0][0] = 1 ;   matriceX[0][1] = 0;   matriceX[0][2] = -1;
    matriceX[1][0] = 2;   matriceX[1][1] = 0 ;  matriceX[1][2] = -2;
    matriceX[2][0] = 1;   matriceX[2][1] = 0;   matriceX[2][2]= -1;

    matriceY[0][0] = 1;  matriceY[0][1] = 2;  matriceY[0][2]= 1;
    matriceY[1][0] = 0;   matriceY[1][1] = 0;  matriceY[1][2] = 0;
    matriceY[2][0] = -1;   matriceY[2][1] = -2;   matriceY[2][2] = -1;

    QImage * img = appliquerConvolution(matriceX,matriceY, imageX);
    delete imageX;

    for(int x = 0; x<image->width(); x++){
        for(int y=0; y<image->height(); y++){
            QRgb colorPixelX = img->pixel(x,y);
            image->setPixel(x,y,colorPixelX);
        }
    }
    delete img;
    delete matriceX;
    delete matriceY;
}

QImage* SobelFilter::appliquerConvolution(int** matrixA, int** matrixB, QImage* image){

    int wimage = image->width();
    int himage = image->height();

    QImage* image_sortie = new QImage(wimage,himage,image->format());
    for(int y = 0; y < image->height(); y++) {
        for(int x = 0; x < image->width(); x++) {
            int redA=0;
            int greenA=0;
            int blueA=0;
            int redB=0;
            int greenB=0;
            int blueB=0;
            for(int l = 0; l < 3; l++) {
                for(int c = 0; c < 3; c++) {
                    int col = y + c - 1;
                    int lig = x + l - 1;
                    if(col < 0){
                        col = -col;
                    }
                    if(lig < 0){
                        lig = -lig;
                    }
                    if(col >= image->height()){
                        col = (image->height() * 2.0) - col - 1;
                    }

                    if(lig >= image->width()){
                        lig = (image->width() * 2.0) - lig - 1;
                    }
                    int tmpRgb = image->pixel(lig, col);

                    redA += ((qRed(tmpRgb))) * matrixA[l][c]/4;
                    greenA += ((qGreen(tmpRgb))) * matrixA[l][c]/4;
                    blueA += ((qBlue(tmpRgb))) * matrixA[l][c]/4;

                    redB += ((qRed(tmpRgb))) * matrixB[l][c]/4;
                    greenB += ((qGreen(tmpRgb))) * matrixB[l][c]/4;
                    blueB += ((qBlue(tmpRgb))) * matrixB[l][c]/4;
                }
            }
            int red = sqrt(redA*redA + redB*redB);
            int green = sqrt(greenA*greenA + greenB*greenB);
            int blue = sqrt(blueA*blueA + blueB*blueB);

            int gray = ( 299*red + 587*green + 114*blue ) /1000;
            image_sortie->setPixel(x, y,qRgb(gray,gray,gray));

        }
    }    
    return image_sortie;
}
